const { contextBridge, ipcRenderer } = require('electron');

// 安全地暴露API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 安全的IPC通信方法
  invoke: (channel, data) => {
    const validChannels = ['ping', 'get-version', 'open-dialog'];
    if (validChannels.includes(channel)) {
      return ipcRenderer.invoke(channel, data);
    }
    throw new Error(`Invalid IPC channel: ${channel}`);
  },
  
  // 安全的监听方法
  on: (channel, callback) => {
    const validChannels = ['notification', 'update-available'];
    if (validChannels.includes(channel)) {
      ipcRenderer.on(channel, (event, ...args) => callback(...args));
    }
  },
  
  // 应用信息
  getAppVersion: () => ipcRenderer.invoke('get-version'),
  
  // 环境信息
  isDevelopment: process.env.NODE_ENV === 'development'
});

// 上下文隔离警告
console.log('[Preload] Context isolation enabled');